package lumis.service.pagepersonalization.doui.serviceinterface;

import lumis.content.service.ContentListServiceInterface;
import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.presentation.PortalModeUtil;
import lumis.portal.presentation.perspective.IPortalMode;
import lumis.portal.serviceinstance.ServiceInstanceConfig;
import lumis.portal.serviceinstance.acl.ServiceInstancePermissions;
import lumis.portal.serviceinterface.IServiceInterfaceRenderRequest;
import lumis.portal.serviceinterface.IServiceInterfaceRenderResponse;
import lumis.portal.serviceinterface.ServiceInterfaceException;
import lumis.portal.transaction.ITransaction;
import lumis.portal.transaction.PortalTransactionFactory;

import java.io.IOException;

/**
 * Widgets holder service interface.
 *
 * @version $Revision: 13321 $ $Date: 2011-08-08 11:32:13 -0300 (Mon, 08 Aug 2011) $
 * @since 6.0.0
 */
public class WidgetListServiceInterface extends ContentListServiceInterface
{
	@Override
	public void render(IServiceInterfaceRenderRequest request, IServiceInterfaceRenderResponse response) throws ServiceInterfaceException,
			PortalException
	{
		String permission = null;
		
		if (PortalModeUtil.getPortalMode(request).getId() == IPortalMode.MODE_PERSONALIZATION)
		{
			permission = "manageHolderDefaultWidgets";
		}
		else
		{
			permission = "personalizePage";
		}

		if (checkPermission(request.getServiceInstanceConfig().getId(),permission))
		{
			try
			{											
				response.getBeforeWriter().write("<script type=\"text/javascript\" src=\"lumis/service/pagepersonalization/script/jquery/jquery-1.4.3.js\"></script>",
						"JQuery-1.4.3");
				response.getBeforeWriter().write(
						"<script type=\"text/javascript\" src=\"lumis/service/pagepersonalization/script/jquery/jquery-ui-1.8.11.custom.min.js\"></script>", "JQueryUI-1.8.11");
				response.getBeforeWriter().write(
						"<script type=\"text/javascript\" src=\"lumis/service/pagepersonalization/script/LumisPagePersonalization.js\"></script>",
						"PagePersonalization");
				response.getBeforeWriter().write(
								"<script type=\"text/javascript\" src=\"lumis/service/pagepersonalization/script/LumisPagePersonalizationFrontend.js\"></script>",
								"PagePersonalizationFrontend");
				response.getBeforeWriter().write("<script type=\"text/javascript\" src=\"lumis/service/pagepersonalization/script/jquery.js\"></script>",
				"JQuery");
			}
			catch (IOException e)
			{
				throw new PortalException(e.getMessage(), e);
			}
		
			super.render(request, response);
		}
	}
	
	/**
	 * Checks if the current session has the specified permission.
	 * @param serviceInstanceId the service instance identifier.
	 * @param permissionId the permission identifier.
	 * @return false if the session does not has the specified permission or true otherwise.
	 * @since 6.0.0
	 */
	private boolean checkPermission(String serviceInstanceId, String permissionId)throws PortalException
	{
		boolean result = true;
		ITransaction transaction = PortalTransactionFactory.getCurrentTransaction();
		ServiceInstanceConfig serviceInstanceConfig = ManagerFactory.getServiceInstanceManager().get(serviceInstanceId, transaction);
		SessionConfig sessionConfig = SessionConfig.getCurrentSessionConfig();
		ServiceInstancePermissions permissions = ManagerFactory.getServiceInstanceAclManager().getPermissions(sessionConfig, serviceInstanceConfig.getServiceId(), transaction);
		if (!ManagerFactory.getServiceInstanceAclManager().checkPermission(sessionConfig, serviceInstanceId, permissions.valueOf(permissionId), transaction))
		{
			result = false;
		}
		return result;
	}
	
}
